/**
 * 公共js
 * Created by htrad0012 on 2017/5/27.
 */


//正则表达式验证执行
export function execRegExp(reg, str) {
  reg = new RegExp(reg);
  return reg.test(str);
}

//判断是否为空
export function isEmpty(str) {
  if (!str || str == null) {
    return true;
  }
  if (typeof str == 'string') {
    return str.trim() == "";
  } else if (typeof str == 'object') {
    for (var strKey in str) {
      return false;
    } {
      return true;
    }
  } else if (typeof str == 'number') {
    return (str + "").trim() == "";
  } else {
    return str.toString().length <= 0;
  }
  // if(str == null || (str + "").trim() == ""){
  //     return true;
  // }
  return false;
}

//判断两个对象是否相等，其中如果object中有属性未定义但是object2中有视为相等
export function isEqual(object, object2) {
  if (object == object2) {
    return true;
  }
  for (var k in object) {
    if (typeof (object[k]) != "undefined") {
      if (typeof (object[k]) == "object") {
        if (!isEqual(object[k], object2[k])) {
          return false
        }
      } else if (object[k] != object2[k]) {
        return false;
      }
    }
  }
  return true;
}

//判断两个对象是否恒等，两个对象必须所有属性和属性值完全相同
export function isAlwaysEqual(object, object2) {
  if (object == object2) {
    return true;
  }
  for (var k in object) {
    if (typeof (object[k]) != "undefined") {
      if (typeof (object[k]) == "object") {
        if (!isEqual(object[k], object2[k])) {
          return false
        }
      } else if (object[k] != object2[k]) {
        return false;
      }
    }
  }
  for (var k in object2) {
    if (typeof (object2[k]) != "undefined") {
      if (typeof (object[k]) == "object") {
        if (!isAlwaysEqual(object[k], object2[k])) {
          return false
        }
      } else if (object[k] != object2[k]) {
        return false;
      }
    }
  }
  return true;
}

//日期字符串(形如20161008)转换为毫秒数
export function getMilliSecond(dateStr) {
  if (dateStr == null) {
    return null;
  }
  return new Date(dateStr.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1/$2/$3')).getTime();
}
//毫秒数转换为日期格式yyyyMMdd
export function milliSendToDate(s) {
  if (s == null) {
    return null;
  }
  var date = new Date(s);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  var d = date.getDate();
  if (m < 10) {
    m = "0" + m;
  }
  if (d < 10) {
    d = "0" + d;
  }
  return "" + y + m + d;
}

//日期+天数=日期的方法
export function getNewDate(dateStr, dayStr) {
  if (dayStr == null) {
    dayStr = 0;
  }
  var dateMilli = getMilliSecond(dateStr);
  var dayMilli = dayStr * 24 * 60 * 60 * 1000;
  var dateMilliAndDayMilli = dateMilli + dayMilli;
  var date = milliSendToDate(dateMilliAndDayMilli);
  console.log(date);
  return date;
}




// /**
//  * 计算若干个月后的日期，并比较两个日期的大小
//  * @param date1 计算前的日期（要求日期格式为yyyyMMdd）
//  * @param date2 计算后的日期（要求日期格式为yyyyMMdd）
//  * @param intervalMonth 相隔的月数
//  * @returns {string} 计算后的日期字符串与date2大小的比较（date1+intervalMonth>date2:true,反之false）
//  */
export function compareDate(date1, intervalMonth, date2) {
  var maxFlag = false;
  if (date1 != null && date1 != '') {
    var yearStr = date1.substr(0, 4);
    var monthStr = date1.substr(4, 2);
    var dayStr = date1.substr(6, 2);
    var month = parseFloat(monthStr);
    var year = parseFloat(yearStr);
    if ((month + intervalMonth) / 12 > 1) {
      year = year + 1;
      month = (month + intervalMonth) % 12;
    } else {
      month = month + intervalMonth;
    }
    if (month < 10) {
      monthStr = '0' + month;
    } else {
      monthStr = month.toString();
    }
    yearStr = year.toString();
    date1 = yearStr + monthStr + dayStr;
    if (!isEmpty(date2) && date1 >= date2) {
      maxFlag = true;
    }
  }
  return maxFlag;
}



/**
 * 校验参数范围为0-100
 * @param rate
 * @param parem 需要校验的字面值
 * @returns {*}
 */
export function checkRate(rate, param) {
  if (isEmpty(rate)) {
    return null;
  }
  if (param == null) {
    param = "利率";
  }
  //先判断是不是数值型
  if (isNaN(rate)) {
    return param + "格式错误";
  }
  //通过转换成浮点型后与0和100比较大小
  var _rate = parseFloat(rate);
  if (_rate < 0 || _rate >= 100) {
    return param + "范围为0-100";
  }
  return "1";
}

export function returnFloat(value) {
  if (value == null || isNaN(value)) {
    return;
  }
  var value = Math.round(parseFloat(value) * 10000) / 10000;
  var xsd = value.toString().split(".");
  if (xsd.length == 1) {
    value = value.toString() + ".0000";
    return value;
  }
  if (xsd.length > 1) {
    if (xsd[1].length < 2) {
      value = value.toString() + "000";
      return value;
    }
    if (2 <= xsd[1].length && xsd[1].length < 3) {
      value = value.toString() + "00";
      return value;
    }
    if (3 <= xsd[1].length && xsd[1].length < 4) {
      value = value.toString() + "0";
      return value;
    }
    if (xsd[1].length = 4) {
      value = value.toString();
      return value;
    }
    if (xsd[1].length > 4) {
      value = value.toFixed(4);
      return value;
    }
    /*     return value;*/
  }
}






//判断是否不为空
export function isNotEmpty(str) {
  if (str != null && (str + "").trim() != "") {
    return true;
  }
  return false;
}

export function getCode(productNo, type) {
  if (productNo && productNo.length == 8) {
    if (type == "draftAttr") return productNo.substr(0, 1); //票据介质
    if (type == "draftType") return productNo.substr(1, 1); //票据类型
    if (type == "busiType") return productNo.substr(2, 3); //业务种类
    if (type == "busiAttr") return productNo.substr(5, 3); //业务属性
    return null;
  }
  return null;
}




/**
 *
 * 根据票号返回票据来源
 */
export function getProductTypeByDraftNumber(draftNumber) {
  var str = draftNumber.substr(0, 1);
  if (str == '1' || str == '2') {
    return 'CS01';
  } else if (str == '5' || str == '6') {
    return 'CS02';
  } else if (str == '7' || str == '8') {
    return 'CS03';
  }
}
// /***
//  * 获取当前日期的前后天数
//  * @param workDay(8位日期,eg:20220713)
//  * @param addDay(前后天数:eg:1,-10)
//  * @returns
//  */
export function getDateStr(workDay, addDay) {
  var yearStr = workDay.substr(0, 4);
  var monthStr = workDay.substr(4, 2);
  var dayStr = workDay.substr(6, 2);
  var dd = new Date(yearStr, monthStr, dayStr);
  dd.setDate(dd.getDate() + addDay);
  var y = dd.getFullYear();
  var m = (dd.getMonth()) < 10 ? "0" + (dd.getMonth()) : (dd
    .getMonth());
  var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  return y + m + d;

}
